package com.user.query.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.user.query.model.User;
import com.user.query.utils.ConstantUtils;

/**
 * Created by zhx on 2015/11/30.
 */
public class BaseController {
	 protected Logger log = Logger.getLogger(getClass());
    
	 
    /** 基于@ExceptionHandler异常处理 */
   // @ExceptionHandler
    public String exceptionHandler(HttpServletRequest request, Exception exception) {
        request.setAttribute("exception", exception);
        // 根据不同错误转向不同页面
        if(exception instanceof RuntimeException) {
            return "error-business";
        }else {
            return "error";
        }
    }

    /**
     * 获取系统当前登录用户对象
     * @param request
     * @return
     */
    public User getCurrentUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(ConstantUtils.USERINFO);
        return user;
    }
    
    /**
     * 获取系统当前登录用户对象
     * @param request
     * @return
     */
    public User getCurrentUser2(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(ConstantUtils.USERINFO);
        return user;
    }
    
    /**
     * 数据绑定（时间、浮点型数据转换）
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
//    	binder.registerCustomEditor(BigDecimal.class, new CustomBigDecimalEditor());
//    	binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
}
